Skip to content

fix(render): store valid diff structure for single-pane views#313

Merged
esmuellert merged 1 commit intomainfrom
dev
Mar 7, 2026
Merged

fix(render): store valid diff structure for single-pane views#313
esmuellert merged 1 commit intomainfrom
dev

Conversation

@esmuellert
Copy link
Owner

Summary

Fixes the crash reported in #309 — cycling tabs with an untracked file open in CodeDiff causes render_diff() to crash on ipairs(nil).

Root Cause

show_untracked_file() (and similar single-pane functions) stored {} as stored_diff_result, missing the required .changes and .moves fields. When resume_diff() reused this value after tab cycling (no changedtick change → no recompute), render_diff() crashed because {}.changes is nil.

Changes

  • side_by_side.lua: Replace {} with {changes={}, moves={}} in show_single_file()
  • inline_view.lua: Same fix in both inline single-file paths
  • layout_spec.lua: Update 2 tests that asserted the old buggy nil behavior
  • tab_cycle_untracked.lua (new): E2E test reproducing the exact PR fix(render): handle nil changes when cycling tabs #309 scenario — verified to fail without fix, pass with fix

Testing

  • All plenary tests pass (500+)
  • E2E test confirms fix: stored_diff_result.changes is {} (not nil) immediately after show_untracked_file()
  • Full tab-cycle path (suspend_diffresume_diffrender_diff) exercised end-to-end

Closes #309

show_untracked_file() and similar single-pane functions stored {} as
stored_diff_result, missing the required .changes and .moves fields.
When resume_diff() reused this value after tab cycling, render_diff()
crashed on ipairs(nil) because {}.changes is nil.

Replace {} with {changes={}, moves={}} in side_by_side.lua and
inline_view.lua to conform to the diff result schema. Add E2E test
that reproduces the exact PR #309 scenario.

Closes #309

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@esmuellert esmuellert enabled auto-merge March 7, 2026 04:03
@esmuellert esmuellert merged commit f6b2906 into main Mar 7, 2026
23 of 25 checks passed
esmuellert added a commit that referenced this pull request Mar 7, 2026
## Summary

Fixes `E5108: Lua: attempt to get length of field 'changes' (a nil
value)` crash in `find_hunk_at_cursor()` when invoking hunk operations
(stage/unstage/discard) while `stored_diff_result.changes` is nil.

## Root Cause

`find_hunk_at_cursor()` checked `#diff_result.changes == 0` without
first verifying `.changes` is non-nil. This crashes when
`stored_diff_result` exists but `.changes` is nil — the same class of
bug fixed in PR #313 for `render_diff()`.

## Changes

- Added nil guard: `if not diff_result.changes or #diff_result.changes
== 0 then`

## Testing

- Same pattern already validated in PR #313
- Single-line defensive fix with no behavioral change when `.changes` is
present
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant